package file_functions;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.Vector;

public class Manager {

	public Manager(String param)
	{
		if (param == null)
			current_file = new File(System.getProperty("user.dir"));
		else
			current_file = new File(param);
	}
	
	public Vector<Files_info> ls_command(boolean print_file, boolean print_parent, int sort_name, int sort_size, int sort_date)
	{
		Vector<Files_info> result = new Vector<Files_info>();
		//Look through all files and directories in current directory and print information about them.
		if (current_file.getParentFile() != null && print_parent)
		{
			Files_info info = new Files_info("..", "", "", false, "");
			result.add(info);
		}
		String[] content = current_file.list();
		
		//separate files from folders
		Vector<String> files = new Vector<String>();
		Vector<String> folders = new Vector<String>();
		for (String str: content)
		{
			File file = new File (current_file.getPath(), str);
			if (file.isDirectory())
				folders.add(str);
			else
				files.add(str);
		}
		
		for (int i = 0; i < folders.size(); i++)
		{
			String to_print;
			File file = new File (current_file.getPath(), folders.get(i));
			
			DateFormat date = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
			to_print = "DIR";
			
			Files_info info2;
			info2 = new Files_info(file.getName(), to_print, date.format(new Date(file.lastModified())), false, "");
				
			result.add(info2);
		}
		
		if (sort_name != 0)
		{
			//sorting names
			if (sort_name == 1)
				Collections.sort(result, new Comp_name_1());
			else
				Collections.sort(result, new Comp_name_2());
		}
		
		if (sort_size != 0)
		{
			//sorting sizes
			//directories are sorted alphabetically from a to z always
			Collections.sort(result, new Comp_name_1());
		}
		
		if (sort_date != 0)
		{
			//sorting names
			if (sort_date == 1)
				Collections.sort(result, new Comp_date_1());
			else
				Collections.sort(result, new Comp_date_2());
		}
		
		Vector<Files_info> result2 = new Vector<Files_info>();
		for (int i = 0; i < files.size(); i++)
		{
			String to_print;
			File file = new File (current_file.getPath(), files.get(i));
			
			DateFormat date = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
			
			to_print = ""+file.length();
			
			if (!print_file)
				continue;
			
			Files_info info2;
			info2 = new Files_info(file.getName(), to_print, date.format(new Date(file.lastModified())), true, file.getAbsolutePath());
				
			result2.add(info2);
		}
		
		if (sort_name != 0)
		{
			//sorting names
			if (sort_name == 1)
				Collections.sort(result2, new Comp_name_1());
			else
				Collections.sort(result2, new Comp_name_2());
		}
		
		if (sort_size != 0)
		{
			//sorting names
			if (sort_size == 1)
				Collections.sort(result2, new Comp_size_1());
			else
				Collections.sort(result2, new Comp_size_2());
		}
		
		if (sort_date != 0)
		{
			//sorting names
			if (sort_date == 1)
				Collections.sort(result2, new Comp_date_1());
			else
				Collections.sort(result2, new Comp_date_2());
		}
		
		//add entries with files
		for (int i = 0; i < result2.size(); i++)
			result.add(result2.get(i));
		return (result);
	}
	
	public void cd_command(String str0)
	{
		if (str0.equals(".."))
		{
			current_file = new File(current_file.getParentFile().getAbsolutePath());
			return;
		}

		File temp_file = new File(current_file, str0);
		
		if (!temp_file.exists())
			return;
		
		if (temp_file.isDirectory())
		{
			current_file = temp_file;
			return;
		}
	}
	
	public String get_name()
	{
		return (current_file.getAbsolutePath());
	}
	
	public boolean delete_command(File parent, String name)
	{
		File file_to_del;
		if (parent == null)
			file_to_del = new File(current_file, name);
		else
			file_to_del = new File(parent, name);
		
		if (!file_to_del.exists())
			return (false);
		
		if (file_to_del.isFile())
		{
			boolean result = file_to_del.delete();
			return (result);
		}
		else
		{
			String[] list =  file_to_del.list();
			boolean result;
			for (String entry_from_list : list)
			{
				result = delete_command(file_to_del, entry_from_list);
				if (!result)
					return (false);
			}
			result = file_to_del.delete();
			if (!result)
				return (false);
		}
		return (true);
	}
	
	public void set_dir(String dir)
	{
		current_file = new File(dir);
	}
	
	public boolean displace(String source, String destination, String name)
	{
		File file = new File(source, name);
		if (destination.startsWith(file.getAbsolutePath()))
		{
			System.out.println(file.getAbsolutePath());
			System.out.println(destination);
			System.out.println("начинается!");
			return (false);
		}
		
		if (file.isFile())
		{
			try
			{
				FileInputStream in = new FileInputStream(file);
				File new_file = new File(destination, name);
				FileOutputStream outs = new FileOutputStream(new_file);
				byte[] read_arr = new byte[BUFF_SIZE];
				int read;
				while ((read = in.read(read_arr)) != -1)
					outs.write(read_arr, 0, read);
				in.close();
				outs.close();
				file.delete();
			}
			catch(IOException ex)
			{
				return (false);
			}
		}
		else
		{
			String[] content = file.list();
			for (String member : content)
			{
				File destin_dir = new File(destination, file.getName());
				destin_dir.mkdir();
				boolean result = displace(file.getAbsolutePath(), destin_dir.getAbsolutePath(), member);
				if (!result)
					return (false);
			}
			file.delete();
		}
		return (true);
	}
	
	public boolean create_folder(String name)
	{
		File file = new File(current_file, name);
		if (file.exists() && file.isDirectory())
			return (false);
		boolean result = file.mkdir();
		if (!result)
			return (false);
		else
			return (true);
	}
	
	public boolean rename(String init, String fin)
	{
		File file = new File(current_file, init);
		File file2 = new File(current_file, fin);
		boolean result = file.renameTo(file2);
		return (result);
	}
	
	public String sort_val(int cur)
	{
		if (cur == 0)
			return ("1");
		else if (cur == 1)
			return ("2");
		else
			return ("1");
	}
	
	public String sort_sign(int cur)
	{
		if (cur == 0)
			return ("");
		else if (cur == 1)
			return ("↓");
		else
			return ("↑");
	}
	private File current_file;
	private int BUFF_SIZE = 4096;
}